package com.lengtouzai.databaseexp.view.friend;

import com.lengtouzai.databaseexp.entity.Friend;
import com.lengtouzai.databaseexp.entity.FriendGroup;
import com.lengtouzai.databaseexp.entity.User;
import com.lengtouzai.databaseexp.view.main.MainView;
import com.lengtouzai.databaseexp.view.newfriend.NewFriendView;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.grid.ItemClickEvent;
import com.vaadin.flow.component.grid.ItemDoubleClickEvent;
import com.vaadin.flow.router.Route;
import io.jmix.core.DataManager;
import io.jmix.core.security.CurrentAuthentication;
import io.jmix.flowui.DialogWindows;
import io.jmix.flowui.component.grid.DataGrid;
import io.jmix.flowui.kit.component.button.JmixButton;
import io.jmix.flowui.model.CollectionLoader;
import io.jmix.flowui.view.*;
import org.springframework.beans.factory.annotation.Autowired;

@Route(value = "friends", layout = MainView.class)
@ViewController("exp_Friend.list")
@ViewDescriptor("friend-list-view.xml")
@LookupComponent("friendsDataGrid")
@DialogMode(width = "64em")
public class FriendListView extends StandardListView<Friend> {
    @ViewComponent
    private CollectionLoader<User> friendsDl;
    @Autowired
    private CurrentAuthentication currentAuthentication;
    @ViewComponent
    private JmixButton excludeBtn;
    @ViewComponent
    private DataGrid<User> friendsDataGrid;
    @Autowired
    private DialogWindows dialogWindows;
    @Autowired
    private DataManager dataManager;

    @Subscribe
    public void onReady(final ReadyEvent event) {
        User current = (User) currentAuthentication.getUser();
        if (!current.getUsername().equals("admin")) {
            friendsDl.setParameter("user", current);
        }
        friendsDl.load();
        excludeBtn.setEnabled(!friendsDataGrid.getSelectedItems().isEmpty());
    }

    @Subscribe("friendsDataGrid")
    public void onFriendsDataGridItemClick(final ItemClickEvent<User> event) {
        excludeBtn.setEnabled(!friendsDataGrid.getSelectedItems().isEmpty());
    }

    @Subscribe("friendsDataGrid")
    public void onFriendsDataGridItemDoubleClick(final ItemDoubleClickEvent<User> event) {
        excludeBtn.setEnabled(!friendsDataGrid.getSelectedItems().isEmpty());
    }

    @Subscribe(id = "addBtn", subject = "clickListener")
    public void onAddBtnClick(final ClickEvent<JmixButton> event) {
        DialogWindow<NewFriendView> window = dialogWindows.view(this, NewFriendView.class).build();
        window.setWidth("75%");
        window.setHeight("75%");

        window.addAfterCloseListener(e -> {
            if (e.closedWith(StandardOutcome.SAVE)) {
                User selected = window.getView().getSelected();
                // get new friend
                Friend newFriend = dataManager.create(Friend.class);
                newFriend.setTarget(selected);

                User current = (User) currentAuthentication.getUser();
                // get default group
                FriendGroup group = dataManager.load(FriendGroup.class)
                        .query("select e from exp_FriendGroup e where e.name = 'Default Group' and e.belongs = :user")
                        .parameter("user", current)
                        .one();
                newFriend.setBelongs(group);
                newFriend.setUser(current);
                dataManager.save(newFriend);
                // add to dataGrid
                friendsDl.load();
            }
        });

        window.open();
    }
}